Kotlin Koans 09 Extension functions

目次

Extension functions (Playground)

Description

Read about extension functions. Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.

拡張関数 を読んでください。拡張関数Int.r()Pair.r()を実装し、IntとPairをRationalNumberに変換してください。

Code

fun Int.r(): RationalNumber = RationalNumber(this, 1)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(first, second)

data class RationalNumber(val numerator: Int, val denominator: Int)

Memo

← Posts